import tarfile
import os
import sys

def extract_tar(tar_path, output_dir):
    if not os.path.isfile(tar_path):
        print(f"TAR file does not exist: {tar_path}")
        return

    try:
        with tarfile.open(tar_path, 'r') as tar_ref:
            tar_ref.extractall(output_dir)
        print(f"Extracted to: {output_dir}")
    except tarfile.ReadError:
        print("Error: It's not a tar file or the file is corrupted")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python untar_util.py <tar_file> <output_dir>")
        sys.exit(1)

    tar_file = sys.argv[1]
    output_dir = sys.argv[2]

    extract_tar(tar_file, output_dir)
